//
//  JXTextFieldView.swift
//  sinaWeibo
//
//  Created by jixiao on 15/12/27.
//  Copyright © 2015年 heimacast. All rights reserved.
//

import UIKit

class JXTextFieldView: UIView {
    
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var discoverTextField: UITextField!
    
    @IBOutlet weak var changeConstraints: NSLayoutConstraint!
    
    @IBAction func cancelClick(sender: AnyObject) {
        
        //取消第一响应者
        self.discoverTextField.resignFirstResponder()
        
        //动态改变textField的约束
        self.changeConstraints.constant=0
        
        UIView.animateWithDuration(0.15) { () -> Void in
            
            //如果视图改变,调用layoutIfNeeded
            self.discoverTextField.layoutIfNeeded()
            
        }
        
        
        
    }
    
    //创建一个方法,加上class变成类方法
    
    class func loadXIB()->JXTextFieldView{
        
        return NSBundle.mainBundle().loadNibNamed("JXTextFieldView", owner: nil, options: nil).last as! JXTextFieldView
        
    }
    @IBAction func startEditing(sender: AnyObject) {
        
        self.changeConstraints.constant=self.cancelBtn.frame.size.width
        
        UIView.animateWithDuration(0.15) { () -> Void in
            
            self.discoverTextField.layoutIfNeeded()
            
        }
        
    }
    
    
    //加载xib文件的时候开始调用方法
    override func awakeFromNib() {
        
        //设置左边的图标
        let leftView=UIImageView()
        
        //
        leftView.image=UIImage(named: "searchbar_textfield_search_icon")
        
        //让textField的左边试图就是这个
        
        //设置frame
        leftView.frame.size = CGSizeMake(frame.height, frame.height)
        
        //这个是左边的图片的填充方式设置在重点
        leftView.contentMode = .Center
        
        discoverTextField.leftView=leftView
        
        //左边的view是否显示出来,一共有4中状态,默认是关闭的,不显示的
        discoverTextField.leftViewMode = .Always
        
    }
    
    
    //这里有没有都一样,方法代表的是截档完成,也就是从nib中加载完数据
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    

}
